<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午9:54
 */

class GameService extends Service {

    /**
     * 根据游戏ID获取一条游戏信息
     */
    public static function getGameById($id){
        $modGame = new ModGame();

        $platform = $modGame->findByPk($id)->attributes;

        return $platform;
    }

    /**
     * 系统首页表头
     */
    public static function indexHeader(){
        return array(
            'GameID'        => array('text' => PubFunc::t('游戏ID')),
            'GameSrc'       => array('text' => PubFunc::t('游戏代号')),
            'GameName'      => array('text' => PubFunc::t('游戏名称')),
            'Remark'        => array('text' => PubFunc::t('简介')),
            'Action'        => array('text' => PubFunc::t('操作')),
        );
    }

    /**
     * 游戏首页数据
     * @param $params
     */
    public static function indexData($params){
        $modGame = new ModGame();
        $result = $modGame->select($params);

        $editText = PubFunc::t('编辑');

        foreach($result as &$v){
        	$v['Action']   .= ' <a href="'.PubFunc::buildUrl('/auth/?r=game/edit').'&id='.$v['GameID'].'">编辑</a>';
        }
        unset($v);

        return $result;
    }

    /**
     * 新增游戏
     * @param $params
     * @return bool
     */
    public static function add($params){
        $modGame = new ModGame();

        $me = AccAuth::me();
        $addBy = $me['UserID'];
        $addTime = date('Y-m-d H:i:s');

        $modGame->GameSrc   = $params['gamesrc'];
        $modGame->GameName  = $params['gamename'];
        $modGame->GameIcon  = $params['icon'];
        $modGame->Remark    = $params['remark'];
        $modGame->AddBy     = $addBy;
        $modGame->AddTime   = $addTime;

        if($modGame->save()){
            $gameId = $modGame->getDbConnection()->getLastInsertID();

            $map = array();
            if(!empty($params['platform']) && is_array($params['platform'])){
                foreach($params['platform'] as $v){
                    $map[] = array(
                        'PlatformID' => $v,
                        'GameID'     => $gameId,
                        'AddBy'      => $addBy,
                        'AddTime'    => $addTime
                    );
                }
            }

            $modPlatformGameMap = new ModPlatformGameMap();
            $modPlatformGameMap->addMapByGame($gameId, $map);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 编辑游戏
     * @param $params
     * @return bool
     */
    public static function edit($params){
        $id = $params['id'];

        $map = array();
        $me = AccAuth::me();
        $addBy = $me['UserID'];
        $addTime = date('Y-m-d H:i:s');

        $attr = array(
            'GameSrc'       => $params['gamesrc'],
            'GameName'      => $params['gamename'],
            'Remark'        => $params['remark'],
            'GameIcon'      => $params['icon'],
            'EditBy'        => $addBy,
            'EditTime'      => $addTime
        );

        if(!empty($params['platform']) && is_array($params['platform'])){
            foreach($params['platform'] as $v){
                $map[] = array(
                    'PlatformID' => $v,
                    'GameID'     => $id,
                    'AddBy'      => $addBy,
                    'AddTime'    => $addTime
                );
            }
        }

        try{
            $modGame = new ModGame();
            $modGame->updateByPk($id, $attr);

            $modPlatformGameMap = new ModPlatformGameMap();
            $modPlatformGameMap->addMapByGame($id, $map);

            return true;
        }catch (Exception $e){
            return false;
        }
    }

    public static function getOptions(){
        $modGame = new ModGame();
        $tmp = $modGame->select();
        $result = array();

        foreach($tmp as $v){
            $result[$v['GameID']] = "[{$v['GameSrc']}] {$v['GameName']}";
        }

        return $result;
    }

    public static function getTypeOptions(){
        return array(
            1 => '页游',
            2 => '手游'
        );
    }
} 